<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>比较运算符</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.operators.bitwise.html">位运算符</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.operators.errorcontrol.html">错误控制运算符</a></div>
 <div class="up"><a href="language.operators.html">运算符</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="language.operators.comparison" class="sect1">
   <h2 class="title">比较运算符</h2>
   <p class="simpara">
    比较运算符，如同它们名称所暗示的，允许对两个值进行比较。还可以参考
    <a href="types.comparisons.html" class="link">PHP 类型比较表</a>看不同类型相互比较的例子。
   </p>
   <table class="doctable table">
    <caption><strong>比较运算符</strong></caption>
    
     <thead>
      <tr>
       <th>例子</th>
       <th>名称</th>
       <th>结果</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>$a == $b</td>
       <td>等于</td>
       <td><strong><code>TRUE</code></strong>，如果类型转换后 <var class="varname"><var class="varname">$a</var></var> 等于 <var class="varname"><var class="varname">$b</var></var>。</td>
      </tr>

      <tr>
       <td>$a === $b</td>
       <td>全等</td>
       <td><strong><code>TRUE</code></strong>，如果 <var class="varname"><var class="varname">$a</var></var> 等于 <var class="varname"><var class="varname">$b</var></var>，并且它们的类型也相同。</td>
      </tr>

      <tr>
       <td>$a != $b</td>
       <td>不等</td>
       <td><strong><code>TRUE</code></strong>，如果类型转换后 <var class="varname"><var class="varname">$a</var></var> 不等于 <var class="varname"><var class="varname">$b</var></var>。</td>
      </tr>

      <tr>
       <td>$a &lt;&gt; $b</td>
       <td>不等</td>
       <td><strong><code>TRUE</code></strong>，如果类型转换后 <var class="varname"><var class="varname">$a</var></var> 不等于 <var class="varname"><var class="varname">$b</var></var>。</td>
      </tr>

      <tr>
       <td>$a !== $b</td>
       <td>不全等</td>
       <td><strong><code>TRUE</code></strong>，如果 <var class="varname"><var class="varname">$a</var></var> 不等于 <var class="varname"><var class="varname">$b</var></var>，或者它们的类型不同。</td>
      </tr>

      <tr>
       <td>$a &lt; $b</td>
       <td>小与</td>
       <td><strong><code>TRUE</code></strong>，如果 <var class="varname"><var class="varname">$a</var></var> 严格小于 <var class="varname"><var class="varname">$b</var></var>。</td>
      </tr>

      <tr>
       <td>$a &gt; $b</td>
       <td>大于</td>
       <td><strong><code>TRUE</code></strong>，如果 <var class="varname"><var class="varname">$a</var></var> 严格大于 <var class="varname"><var class="varname">$b</var></var>。</td>
      </tr>

      <tr>
       <td>$a &lt;= $b</td>
       <td>小于等于</td>
       <td><strong><code>TRUE</code></strong>，如果 <var class="varname"><var class="varname">$a</var></var> 小于或者等于 <var class="varname"><var class="varname">$b</var></var>。</td>
      </tr>

      <tr>
       <td>$a &gt;= $b</td>
       <td>大于等于</td>
       <td><strong><code>TRUE</code></strong>，如果 <var class="varname"><var class="varname">$a</var></var> 大于或者等于 <var class="varname"><var class="varname">$b</var></var>。</td>
      </tr>

     </tbody>
    
   </table>

   <p class="para">
    如果比较一个数字和字符串或者比较涉及到数字内容的字符串，则字符串会被<a href="language.types.string.html#language.types.string.conversion" class="link">转换为数值</a>并且比较按照数值来进行。此规则也适用于
    <a href="control-structures.switch.html" class="link">switch</a> 语句。当用 === 或 !==
	进行比较时则不进行类型转换，因为此时类型和数值都要比对。
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;0&nbsp;==&nbsp;0&nbsp;-&gt;&nbsp;true<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #DD0000">"1"&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">"01"</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;1&nbsp;==&nbsp;1&nbsp;-&gt;&nbsp;true<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #DD0000">"10"&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">"1e1"</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;10&nbsp;==&nbsp;10&nbsp;-&gt;&nbsp;true<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">100&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #DD0000">"1e2"</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;100&nbsp;==&nbsp;100&nbsp;-&gt;&nbsp;true<br /><br /></span><span style="color: #007700">switch&nbsp;(</span><span style="color: #DD0000">"a"</span><span style="color: #007700">)&nbsp;{<br />case&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"0"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;break;<br />case&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">:&nbsp;</span><span style="color: #FF8000">//&nbsp;never&nbsp;reached&nbsp;because&nbsp;"a"&nbsp;is&nbsp;already&nbsp;matched&nbsp;with&nbsp;0<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;break;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>

   <p class="para">
    对于多种类型，比较运算符根据下表比较（按顺序）。
   </p>
   <table id="language.operators.comparison.types" class="doctable table">
    <caption><strong>比较多种类型</strong></caption>
    
     <thead>
      <tr>
       <th>运算数 1 类型</th>
       <th>运算数 2 类型</th>
       <th>结果</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><span class="type"><a href="language.types.null.html" class="type null">null</a></span> 或 <span class="type"><a href="language.types.string.html" class="type string">string</a></span></td>
       <td><span class="type"><a href="language.types.string.html" class="type string">string</a></span></td>
       <td>将 <strong><code>NULL</code></strong> 转换为 &quot;&quot;，进行数字或词汇比较</td>
      </tr>

      <tr>
       <td><span class="type"><a href="language.types.boolean.html" class="type bool">bool</a></span> 或 <span class="type"><a href="language.types.null.html" class="type null">null</a></span></td>
       <td>任何其它类型</td>
       <td>转换为 <span class="type"><a href="language.types.boolean.html" class="type bool">bool</a></span>，<strong><code>FALSE</code></strong> &lt; <strong><code>TRUE</code></strong></td>
      </tr>

      <tr>
       <td><span class="type"><a href="language.types.object.html" class="type object">object</a></span></td>
       <td><span class="type"><a href="language.types.object.html" class="type object">object</a></span></td>
       <td>
        内置类可以定义自己的比较，不同类不能比较，相同类和数组同样方式比较属性（PHP
        4 中），PHP 5 有其自己的<a href="language.oop5.object-comparison.html" class="link">说明</a>
       </td>
      </tr>

      <tr>
       <td><span class="type"><a href="language.types.string.html" class="type string">string</a></span>，<span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> 或 <span class="type"><a href="language.pseudo-types.html#language.types.number" class="type number">number</a></span></td>
       <td><span class="type"><a href="language.types.string.html" class="type string">string</a></span>，<span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> 或 <span class="type"><a href="language.pseudo-types.html#language.types.number" class="type number">number</a></span></td>
       <td>将字符串和资源转换成数字，按普通数学比较</td>
      </tr>

      <tr>
       <td><span class="type"><a href="language.types.array.html" class="type array">array</a></span></td>
       <td><span class="type"><a href="language.types.array.html" class="type array">array</a></span></td>
       <td>
        具有较少成员的数组较小，如果运算数 1 中的键不存在于运算数
        2 中则数组无法比较，否则挨个值比较（见下例）
       </td>
      </tr>

      <tr>
       <td><span class="type"><a href="language.types.object.html" class="type object">object</a></span></td>
       <td>任何其它类型</td>
       <td><span class="type"><a href="language.types.object.html" class="type object">object</a></span> 总是更大</td>
      </tr>

      <tr>
       <td><span class="type"><a href="language.types.array.html" class="type array">array</a></span></td>
       <td>任何其它类型</td>
       <td><span class="type"><a href="language.types.array.html" class="type array">array</a></span> 总是更大</td>
      </tr>

     </tbody>
    
   </table>

   <p class="para">
    <div class="example" id="example-119">
     <p><strong>Example #1 标准数组比较代码</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;数组是用标准比较运算符这样比较的<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">standard_array_compare</span><span style="color: #007700">(</span><span style="color: #0000BB">$op1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$op2</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$op1</span><span style="color: #007700">)&nbsp;&lt;&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$op2</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$op1&nbsp;&lt;&nbsp;$op2<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$op1</span><span style="color: #007700">)&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$op2</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$op1&nbsp;&gt;&nbsp;$op2<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$op1&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$op2</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;uncomparable<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">$op2</span><span style="color: #007700">[</span><span style="color: #0000BB">$key</span><span style="color: #007700">])&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">$val&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">$op2</span><span style="color: #007700">[</span><span style="color: #0000BB">$key</span><span style="color: #007700">])&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;$op1&nbsp;==&nbsp;$op2<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>

   <p class="para">
    参见 <span class="function"><a href="function.strcasecmp.html" class="function">strcasecmp()</a></span>，<span class="function"><a href="function.strcmp.html" class="function">strcmp()</a></span>，<a href="language.operators.array.html" class="link">数组运算符</a>和<a href="language.types.html" class="link">类型</a>章节。
   </p>

   <div class="warning"><strong class="warning">Warning</strong>
    <h1 class="title">比较浮点数</h1>

    <p class="para">
     由于浮点数 <span class="type"><a href="language.types.float.html" class="type float">float</a></span> 的内部表达方式，不应比较两个浮点数是否相等。
    </p>

    <p class="para">
     更多信息参见 <span class="type"><a href="language.types.float.html" class="type float">float</a></span>。
    </p>
   </div>

  <div class="sect2" id="language.operators.comparison.ternary">
    <h3 class="title">三元运算符</h3>
    <p class="para">
     另一个条件运算符是“?:”（或三元）运算符 。
     <div class="example" id="example-120">
      <p><strong>Example #2 赋默认值</strong></p>
      <div class="example-contents">
 <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;</span><span style="color: #FF8000">//&nbsp;Example&nbsp;usage&nbsp;for:&nbsp;Ternary&nbsp;Operator<br />&nbsp;</span><span style="color: #0000BB">$action&nbsp;</span><span style="color: #007700">=&nbsp;(empty(</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">]))&nbsp;?&nbsp;</span><span style="color: #DD0000">'default'&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">];<br /><br />&nbsp;</span><span style="color: #FF8000">//&nbsp;The&nbsp;above&nbsp;is&nbsp;identical&nbsp;to&nbsp;this&nbsp;if/else&nbsp;statement<br />&nbsp;</span><span style="color: #007700">if&nbsp;(empty(</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">]))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$action&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'default'</span><span style="color: #007700">;<br />&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$action&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'action'</span><span style="color: #007700">];<br />&nbsp;}<br /><br />&nbsp;</span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
     表达式 <em>(expr1) ? (expr2) : (expr3)</em> 在
     <span class="replaceable">expr1</span> 求值为 <strong><code>TRUE</code></strong>
     时的值为 <span class="replaceable">expr2</span>，在
     <span class="replaceable">expr1</span> 求值为 <strong><code>FALSE</code></strong> 时的值为
     <span class="replaceable">expr3</span>。
    </p>
    <p class="para">
     自 PHP 5.3 起，可以省略三元运算符中间那部分。表达式
     <em>expr1 ?: expr3</em> 在
     <span class="replaceable">expr1</span> 求值为 <strong><code>TRUE</code></strong> 时返回
	 <span class="replaceable">expr1</span>，否则返回
     <span class="replaceable">expr3</span>。
    </p>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      注意三元运算符是个语句，因此其求值不是变量，而是语句的结果。如果想通过引用返回一个变量这点就很重要。在一个通过引用返回的函数中语句
      <em>return $var == 42 ? $a : $b;</em> 将不起作用，以后的 PHP
      版本会为此发出一条警告。
     </span>
    </p></blockquote>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      建议避免将三元运算符堆积在一起使用。当在一条语句中使用多个三元运算符时会造成
      PHP 运算结果不清晰：
      <div class="example" id="example-121">
       <p><strong>Example #3 不清晰的三元运算符行为</strong></p>
       <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;乍看起来下面的输出是&nbsp;'true'<br /></span><span style="color: #007700">echo&nbsp;(</span><span style="color: #0000BB">true</span><span style="color: #007700">?</span><span style="color: #DD0000">'true'</span><span style="color: #007700">:</span><span style="color: #0000BB">false</span><span style="color: #007700">?</span><span style="color: #DD0000">'t'</span><span style="color: #007700">:</span><span style="color: #DD0000">'f'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;然而，上面语句的实际输出是't'，因为三元运算符是从左往右计算的<br /><br />//&nbsp;下面是与上面等价的语句，但更清晰<br /></span><span style="color: #007700">echo&nbsp;((</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #DD0000">'true'&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">'false'</span><span style="color: #007700">)&nbsp;?&nbsp;</span><span style="color: #DD0000">'t'&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">'f'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;here,&nbsp;you&nbsp;can&nbsp;see&nbsp;that&nbsp;the&nbsp;first&nbsp;expression&nbsp;is&nbsp;evaluated&nbsp;to&nbsp;'true',&nbsp;which<br />//&nbsp;in&nbsp;turn&nbsp;evaluates&nbsp;to&nbsp;(bool)true,&nbsp;thus&nbsp;returning&nbsp;the&nbsp;true&nbsp;branch&nbsp;of&nbsp;the<br />//&nbsp;second&nbsp;ternary&nbsp;expression.<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
       </div>

      </div>
     </p>
    </p></blockquote>
    </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.operators.bitwise.html">位运算符</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.operators.errorcontrol.html">错误控制运算符</a></div>
 <div class="up"><a href="language.operators.html">运算符</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
